//
//  ZXMainViewController.swift
//  ZXWeibo
//
//  Created by mac on 15/11/23.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class ZXMainViewController: UITabBarController {
    
    func composeClick()
    {
        print("点了")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        // 使用自定义的tabBar来设置tabBar按钮上面的位置，再添加中间的按钮
        //也可以使用展位控制器
//        设置自定义的tabbar
//       let newTabBar = ZXTabBar()
////        KVC
//        setValue(newTabBar, forKey: "tabBar")
//        
//        newTabBar.composeButton.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
//设置图标颜色相同
        tabBar.tintColor = UIColor.orangeColor()
        
        //首页
        let homeVc = ZXHomeViewController()
        
        addChildViewController(homeVc, title: "首页", imageName: "tabbar_home")
        
        
        //消息
        let messageVc = ZXMessageViewController()
        
         addChildViewController(messageVc, title: "消息", imageName: "tabbar_message_center")
        
        //占位控制器
        addChildViewController(UIViewController())
        
        //发现
        let discoverVc = ZXDiscoverViewController()
        
        addChildViewController(discoverVc, title: "发现", imageName: "tabbar_discover")
        
        //我
        let profileVc = ZXProfileViewController()
        
        addChildViewController(profileVc, title: "我", imageName: "tabbar_profile")

    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //设置撰写按钮的frame
        let width = tabBar.bounds.size.width / 5
        let frame = CGRect(x: 2 * width, y: 0, width: width + 10, height: tabBar.bounds.height)
        composeButton.frame = frame
    }

    /**
    自定义tabbar的子控制器
    
    :param: controller 根控制器
    :param: title      子控制器title的名字
    :param: imageName  子控制器的图片名称
    */
    func addChildViewController(controller: UIViewController, title:String,imageName:String)
    {
        //消息
        controller.title = title
        //设置tabBarItem按钮的图片
        controller.tabBarItem.image = UIImage(named: imageName)
        
        //法1  法2上面
//设置图标颜色和图片原有颜色相同
//        let highlightedImageName = imageName + "_highlighted"
//        //让选中的图片使用原来的颜色，不要系统帮我们渲染 AlwaysOriginal 永远都是原来的
//        let highlightedImage = UIImage(named: highlightedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
//        //设置高亮图片
//        controller.tabBarItem.selectedImage = highlightedImage
//        //设置标题的颜色
//        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Highlighted)
        
        //将子控制器添加到根控制器
        addChildViewController(UINavigationController(rootViewController: controller))
    }

 
    //    懒加载
    lazy var composeButton: UIButton = {
        let button = UIButton()
        //设置参数
        //设置按钮背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //添加到tabbar里面
        self.tabBar.addSubview(button)
        
        //添加点击事件
        button.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
        }()

}
